package acheng1314.cn.a3dbuild2.view.activity.presenter;


import com.acheng.achengutils.gsonutil.GsonUtils;
import com.acheng.achengutils.mvp.presenter.BasePresenter;
import com.acheng.achengutils.utils.StringUtils;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import java.io.File;

import acheng1314.cn.a3dbuild2.MyApplication;
import acheng1314.cn.a3dbuild2.bean.BaseResponseBean;
import acheng1314.cn.a3dbuild2.bean.ModelUrlBean;
import acheng1314.cn.a3dbuild2.hostApi.MyApi;
import acheng1314.cn.a3dbuild2.view.activity.viewcontroller.AppWebActivityViewController;

/**
 * Created by pc859107393 on 2016/9/13 0013.
 */
public class AppWebActivityPresenter extends BasePresenter<AppWebActivityViewController> {
    /**
     * 在子类的构造函数中，设定参数为model，这时候可以presenter调用接口来实现对界面的操作。
     *
     * @param model
     */
    public AppWebActivityPresenter(AppWebActivityViewController model) {
        super(model);
    }

    @Override
    public void initData() {

    }

    public void uploadPic(String fileAbsolutePath, ModelUrlBean modelUrlBean) {
        if (StringUtils.isEmpty(fileAbsolutePath) || null == modelUrlBean) {
            model.showDailog("参数异常");
            return;
        }
        HttpParams params = new HttpParams();
        params.put("upfile", new File(fileAbsolutePath));
        params.put("uid", modelUrlBean.getUid());
        params.put("mid", modelUrlBean.getMid());
        params.put("longit", modelUrlBean.getLongit());
        params.put("latitu", modelUrlBean.getLatitu());
        params.put("altitu", modelUrlBean.getAltitu());
        params.put("token", modelUrlBean.getToken());

        RxVolley.post(MyApi.picUpload, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                super.onSuccess(t);
                try {
                    if (null == t) {
                        MyApplication.getInstance().outLog("输出", t);
                        model.showDailog("未知错误！");
                        return;
                    }
                    BaseResponseBean bean = new GsonUtils().toBean(t, BaseResponseBean.class);
                    if (bean.getCode() == 0) {
                        model.showDailog("上传成功");
                    } else {
                        model.showDailog("上传失败，" + bean.getMsg());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    model.showDailog("上传失败，未知错误");
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                model.disProgressD();
            }

            @Override
            public void onPreStart() {
                super.onPreStart();
                model.showProgressD();
            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                model.showDailog("获取数据失败，请检查网络");
            }
        });

    }
}
